package com.certification.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.certification.common.utils.Constant;
import com.certification.dto.CorrectImproveDTO;
import com.certification.service.CorrectImproveService;
import com.certification.service.SysUserRoleService;
import com.certification.vo.CorrectImproveVO;
import com.certification.vo.FindCertVO;
import com.certification.vo.PageVO;
import com.certification.vo.ResponseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author copa
 * @createDate 2022-03-30 22:17
 * @function
 */
@RestController
@Api(tags = {"流程：整改提高"})
public class CorrectImproveController extends AbstractController {

    @Resource
    private CorrectImproveService correctImproveService;

    @Resource
    private SysUserRoleService sysUserRoleService;

    @GetMapping("improvePage")
    @ApiOperation(value = "查看整改提高分页")
    public ResponseMessage<IPage<CorrectImproveVO>> getImprovePage(FindCertVO findCertVO) {
        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
            if (roleId == Constant.ROLE_PRINCIPAL) {
                return correctImproveService.getImprovePage(Math.toIntExact(getUserId()), findCertVO);
            }
        }
        return ResponseMessage.errorMsg("该角色不能调用该接口~");
    }

    @GetMapping("improve")
    @ApiOperation(value = "查看整改提高(包括详情也调用这个接口)")
    public ResponseMessage<CorrectImproveVO> getImprove(@RequestParam("proId") Integer proId) {
        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
            if (roleId == Constant.ROLE_PRINCIPAL) {
                return correctImproveService.getImprove(proId, Math.toIntExact(getUserId()));
            }
        }
        return ResponseMessage.errorMsg("该角色不能调用该接口~");
    }

    @PostMapping("improve")
    @ApiOperation(value = "提交整改提高")
    public ResponseMessage<String> addImprove(@RequestBody CorrectImproveDTO correctImproveDTO) {
        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
            if (roleId == Constant.ROLE_PRINCIPAL) {
                return correctImproveService.addImprove(correctImproveDTO, Math.toIntExact(getUserId()));
            }
        }
        return ResponseMessage.errorMsg("该角色不能调用该接口~");
    }

    @PutMapping("improve")
    @ApiOperation(value = "修改整改提高")
    public ResponseMessage<String> putImprove(@RequestBody CorrectImproveDTO correctImproveDTO) {
        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
            if (roleId == Constant.ROLE_PRINCIPAL) {
                return correctImproveService.putImprove(correctImproveDTO, Math.toIntExact(getUserId()));
            }
        }
        return ResponseMessage.errorMsg("该角色不能调用该接口~");
    }


}
